package me.zhengjie.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.domain.PalletService;
import me.zhengjie.domain.ProcesscardService;
import me.zhengjie.domain.UnbindingService;
import me.zhengjie.dto.NotAssignedOrderInfo;
import me.zhengjie.dto.Resp;
import me.zhengjie.dto.sqlServer.Pallet;
import me.zhengjie.dto.sqlServer.Processcard;
import me.zhengjie.dto.sqlServer.UnbindingOrder;
import me.zhengjie.dto.sqlServer.UnbindingOrderDetail;
import me.zhengjie.repository.PalletRepository;
import me.zhengjie.repository.ProcesscardRepository;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequiredArgsConstructor
@Api(tags = "拆解作业")
@RequestMapping("/api/unbinding")
public class UnbindingController {

    private final PalletService palletService;
    private final ProcesscardService processcardService;
    private final UnbindingService unbindingService;

    @Log("手动获取拆解信息")
    @ApiOperation("手动获取拆解信息")
    @GetMapping(value = "/getMsg/{palletCode}")
    public Resp getMsg(@PathVariable("palletCode") String palletCode){
        try {
            Pallet pallet = palletService.findByPalletCode(palletCode);
            Processcard processcard = processcardService.findProcesscardById(pallet.getProcesscardId());
            return Resp.builder()
                    .msg("成功")
                    .status(200)
                    .data("inventory",processcard)
                    .build();
        } catch (Exception e) {
            e.printStackTrace();
            return Resp.builder()
                    .msg("没有查询到物料信息")
                    .data("错误信息",e)
                    .status(500)
                    .build();
        }
    }

    @Log("获取当前扫码口的拆解单")
    @ApiOperation("获取当前扫码口的拆解单")
    @GetMapping("/getNewUnbindingOrder")
    public Resp getUnbindingOrder(){
        try {
            List<UnbindingOrder> orders = unbindingService.findNotAssignedOrder();
            if(!ObjectUtils.isEmpty(orders)){
                UnbindingOrder notAssignedOrder = unbindingService.findNotAssignedOrder().get(0);
                NotAssignedOrderInfo notAssignedOrderInfo = unbindingService.findNotAssignedOrderInfo(notAssignedOrder);
                return Resp.builder()
                        .msg("获取未分配拆解单成功")
                        .status(200)
                        .data("notAssignedOrderInfo", notAssignedOrderInfo )
                        .build();
            }else {
                return Resp.builder()
                        .msg("没有待分配拆解单")
                        .status(300)
                        .build();
            }

        } catch (Exception e) {
            return Resp.builder()
                   .msg("获取拆解单失败")
                   .data("错误信息",e)
                   .status(500)
                   .build();
        }
    }

    @Log("分配当前待拆解的拆解单")
    @ApiOperation("分配当前待拆解的拆解单")
    @GetMapping("/getNewUnbindingOrder/{unbindingOrderCode}/{stationName}")
    public Resp chooseUnbindingStation(@PathVariable("unbindingOrderCode") String unbindingOrderCode, @PathVariable("stationName") String stationName){
        Integer result = unbindingService.chooseStation(unbindingOrderCode, stationName);
        switch (result) {
            case 0:
                return Resp.builder()
                        .msg("分配成功")
                        .status(200)
                        .build();
            case 1:
                return Resp.builder()
                        .msg("拆解单已选择进入缓存架")
                        .status(300)
                        .build();
            case 2:
                return Resp.builder()
                        .msg("已分配拆解站")
                        .status(300)
                        .build();
            case 3:
                return Resp.builder()
                        .msg("当前工作站已存在拆解单")
                        .status(300)
                        .build();
        }
        return null;
    }

    @Log("获取缓存架以及拆解站信息")
    @ApiOperation("获取缓存架以及拆解站信息")
    @GetMapping("/getAllStationStatus")
    public Resp getAllStationStatus(){
        return Resp.builder()
                .msg("拆解站状态获取成功")
                .data("UnbindingStations", unbindingService.getAllStationStatus())
                .status(200)
                .build();
    }


    @Log("获取所有拆解站信息")
    @ApiOperation("获取所有拆解站信息")
    @GetMapping("/getUnbindingStationStatus")
    public Resp getUnbindingStationStatus(){
        return Resp.builder()
                .msg("拆解站状态获取成功")
                .data("UnbindingStations", unbindingService.getUnbindingStationStatus())
                .status(200)
                .build();
    }



    @Log("获取所有缓存架信息")
    @ApiOperation("获取所有缓存架信息")
    @GetMapping("/getAllCacheRackInfo")
    public Resp getAllCacheRackInfo(){
        return Resp.builder()
                .msg("获取缓存架信息成功")
                .data("cacheRacks", unbindingService.getAllCacheRackInfo())
                .status(200)
                .build();
    }

    @Log("释放缓存架")
    @ApiOperation("释放缓存架")
    @GetMapping("/freeCacheRack/{cacheRackName}/{unbindingStationName}")
    public Resp freeCacheRack(@PathVariable("cacheRackName") String cacheRackName, @PathVariable("unbindingStationName") String unbindingStationName){
        Integer result = unbindingService.freeCacheRack(cacheRackName, unbindingStationName);
        switch (result) {
            case 0:
                return Resp.builder()
                        .msg(cacheRackName+"缓存架没有载盘")
                        .status(300)
                        .build();
            case 1:
                return Resp.builder()
                        .msg(unbindingStationName+"拆解站已被占用")
                        .status(300)
                        .build();
            case 2:
                return Resp.builder()
                        .msg(cacheRackName+"释放成功")
                        .status(200)
                        .build();

            case 3:
                return Resp.builder()
                        .msg(cacheRackName+"该拆解单还没完全进入缓存架")
                        .status(300)
                        .build();
        }
        return null;

    }

    @GetMapping("/getCacheRacks")
    public Resp getAllCacheRacks(){
        return Resp.builder()
                .msg("获取成功")
                .status(200)
                .data("CacheRacks",unbindingService.findCacheRacks())
                .build();
    }

    @GetMapping("/clearUnbindingStation/{stationName}")
    public Resp clearUnbindingStation(@PathVariable String stationName){
        try {
            unbindingService.clearUnbindingStation(stationName);
            return Resp.builder()
                    .msg("清空站点数据")
                    .status(200)
                    .build();
        } catch (Exception e) {
            return Resp.builder()
                    .msg("清空站点数据失败，失败原因："+e)
                    .status(500)
                    .build();
        }
    }
}
